package com.example.xutils1.adapter;

import java.util.ArrayList;
import java.util.List;

import com.example.xutils1.bean.PagerItem;
import com.lidroid.xutils.BitmapUtils;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class HeaderViewAdapter extends PagerAdapter {

	private ArrayList<PagerItem> mData = new ArrayList<PagerItem>();

	private LayoutInflater mInflater;
	private BitmapUtils mBitmapUtils;

	public HeaderViewAdapter(Context ctx) {
		mInflater = LayoutInflater.from(ctx);
		mBitmapUtils = new BitmapUtils(ctx);
	}

	public boolean isEmpty() {
		return mData.isEmpty();
	}

	public void setData(List<PagerItem> item) {
		if (item != null) {
			mData.clear();
			mData.addAll(item);
		}
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {

		ImageView img = new ImageView(mInflater.getContext());
		img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		img.setScaleType(ScaleType.CENTER_CROP);

		PagerItem item = mData.get(position);

		mBitmapUtils.display(img, item.iphoneimg);

		container.addView(img);

		return img;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}
}